<?php
namespace Shop\Controller;
use Think\Controller;

class CouponController extends ShopController{
	/**
     * 后台控制器初始化
     */

    public function __construct(){
		 parent::__construct(); 

		$this->coupon = M("coupon");$this->coupon_data = M("coupon_data");

    }
	public function index(){ 
		//$name = jj$_GET['name'];
    	$map['shop_id'] = $this->shop_id;
		$start = $_POST['starttime'];
		$endtime = $_POST['endtime'];
		if($start)
		{
		  $map['starttime'] = array('gt',$start);
				$this->assign('starttime',$start);
		}
		if($endtime)
		{
			
			$map['endtime'] = array('lt',$endtime);
			$this->assign('endtime',$endtime);
		}
        $rows = $_GET['rows'] ? $_GET['rows'] : 10;

		$count      = $this->coupon->where($map)->count();
		$Page       = new \Think\Page($count,$rows);  
	
        $list = $this->coupon->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('starttime asc')->select();
		if(!empty($list))
		{
			foreach($list as $k=>$v)
			{
                $list[$k]['ling'] = $this->coupon_data->where(array('coupon_id'=>$v['id']))->count();
			}
		}
		$this->assign('list',$list);
		$this->assign('page',$Page);
		$this->display();
	}

	//卡券验证页面
	public function check_ticket(){
			
			$this->display();
		
    }
	//
	public function do_ticket(){
		    $status['error'] =1;
      		$tick = trim($_POST['tick']);
			$map['shop_id'] = $this->shop_id;
			$map['code'] = $tick;
			$map['status'] = 0;
			$rs = $this->coupon_data->where($map)->find();
			if($rs['type']==1)//体验券
			{
			  if($time>$rs['endtime'])
			  {
				  			  $status['msg'] = '卡券已过期';
                     $this->ajaxReturn($status);
				
			  }
			  if($rs['times']<1)
			  {
            
					$status['msg'] = '体验次数已经用完';
                     $this->ajaxReturn($status);
			  }
              $this->coupon_data->where($map)->setDec('times');
			  $status['msg'] = '消费成功,剩余体验次数: '.$rs['times']-1;
			}
			else if($rs['type']==2)//代金券
			{
			  $time = strtotime(date("Y-m-d"));
			  if($time>$rs['endtime'])
				{
				 	 $status['msg'] = '卡券已过期';
                     $this->ajaxReturn($status);
				}
						 $this->coupon_data->where($map)->save(array('status'=>1,'sumtime'=>time()));
				 $status['msg'] ='验证成功';
			}else
			{
		
				    $status['msg'] = '卡券不存在';
                     $this->ajaxReturn($status);
			}
			$status['error'] =0;
							    
                     $this->ajaxReturn($status);
	}
	//添加 编辑 卡券
	public function add_ticket(){
		if(IS_POST){ 

            $time = strtotime(date("Y-m-d"));
			$type = $_REQUEST['type'];
			$id = $_REQUEST['id'];
			$shop = $_POST['shop'];
			$total = $_POST['total'];
			$starttime = strtotime($_POST['starttime']);
			$title = trim($_POST['title']);
			$endtime = strtotime($_POST['endtime']);
			$get_limit = intval($_POST['get_limit']) ? intval($_POST['get_limit']) : 1;
		
			$data['starttime'] = $starttime;
			$data['endtime'] = $endtime;
			$data['title'] = $title;
			$code = mt_rand(0,9);
			$data['code'] = $code.time();
			$data['status'] = 0;
			$data['total'] = $total;
			$data['shop_id'] = $this->shop_id;
			$data['type'] = $type;
			$data['get_limit'] = $get_limit;
			$data['imgurl'] = $_POST['thumb'];
			if($type==1)
			{
			
               $data['times'] = $_POST['times'] ? $_POST['times'] : 1;
			} 
			if($type==2)
			{
			  
             $data['jianmian'] = $_POST['jianmian'];
			 $data['dikou'] = $_POST['digggou']; //print_R($data);exit;
			}
			if($id>0){//编辑数据
               $cid = $this->coupon->where(array('id'=>$id))->save($data);
					if(false !== $cid)
					{
		   
						$this->success('编辑成功！', U('index'));
				
						
					}
					else{
						$this->error('操作失败');
					}
			}else{
			//添加数据
					$cid = $this->coupon->add($data);
					if($cid>0)
					{
		   
						$this->success('添加成功！', U('index'));
				
						
					}
					else{
						$this->error('操作失败');
					}
			}
		}else{
			$type = $_GET['type'];
			$id = $_GET['id'];
			if($id>0)
			{
				$data = $this->coupon->where(array('id'=>$id))->find();
				$data['starttime'] = date("Y-m-d",$data['starttime']);
				$data['endtime'] = date("Y-m-d",$data['endtime']);
				$this->assign('data',$data);
				$this->assign('id',$id);
			}
			$this->assign('sessionid',session_id());
			$this->assign('type',$type);
			$this->display();
		}
    }

		//修改库存
	public function stock(){
			$id = intval($_POST['id']); 
			$type = intval($_POST['qtype']);
			$num = intval($_POST['qmun']);
						if(!$id)
		{
							$status['error'] = 1;
		    $this->ajaxReturn($status);		
		}
			if($num<0)
		{
							$status['error'] = 1;
		    $this->ajaxReturn($status);		
		}

			if($type==1)
			{
              $this->coupon->where(array('id'=>$id))->setInc('total',$num);
			}else
		    {
              $this->coupon->where(array('id'=>$id))->setDec('total',$num);

		    }
			$status['error'] = 0;
		    $this->ajaxReturn($status);		
    }

		//删除卡券
	public function del_ticket(){
			$id = intval($_POST['id']); 
			$id = $this->coupon->where(array('id'=>$id))->delete();
			$status['error'] = 1;
		    $this->ajaxReturn($status);		
    }
		//删除核销卡券
	public function del_data(){
			$id = intval($_POST['id']); 
			$id = $this->coupon_data->where(array('id'=>$id))->delete();
			$status['error'] = 1;
		    $this->ajaxReturn($status);		
    }

//卡券领取记录
	public function ling(){	        
			$id = intval($_GET['id']);
			$map['shop_id'] = $this->shop_id;
			if($id>0)
		{
               $map['coupon_id'] = $id;	
		}
								
      
			$rows = $_GET['rows'] ? $_GET['rows'] : 10;

			$count      = $this->coupon_data->where($map)->count();
			$Page       = new \Think\Page($count,$rows);  
	
            $list = $this->coupon_data->where($map)->order('addtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			if(!empty($list))
			{
                foreach($list as $key=>$val)
				{
					$rs = $this->coupon->where(array('id'=>$val['coupon_id']))->find();
                    $list[$key]['type'] = $rs['type'];
					$list[$key]['code'] = $rs['code'];
					$list[$key]['sumtime'] = date("Y-m-d",$rs['sumtime']);
					$list[$key]['addtime'] = date("Y-m-d",$val['addtime']);
					unset($rs);
				}
						
			}
			$this->assign('page',$Page);
			$this->display();
		
    }

}

?>